package main

import (
	"context"
	"demo1/proto"
	"errors"
	"github.com/micro/go-micro/v2"
)
var students = map[string]*proto.Student{
	"ztind":&proto.Student{Name:"ztind",Age:18,Address:"china"},
	"jimmy":&proto.Student{Name:"jimmy",Age:19,Address:"Jpan"},
	"tom":&proto.Student{Name:"tom",Age:20,Address:"USA"},
}

type StudentManage struct{}
func (this *StudentManage)GetStudent(ctx context.Context,in *proto.StudentRequest,out *proto.Student) error{
	student := students[in.Name]
	if student.Name == ""{
		return errors.New("不存在")
	}
	*out = *student
	return nil
}
func (this *StudentManage)GetAllStudent(ctx context.Context,in *proto.StudentRequest,out proto.StudentService_GetAllStudentStream)error{
	for _,item := range students{
		out.Send(item)
	}
	return nil
}
func main() {
	server := micro.NewService(micro.Name("student.server"),micro.Version("v1.0.0"))
	server.Init()
	//注册服务
	proto.RegisterStudentServiceHandler(server.Server(),&StudentManage{})
	if err :=server.Run();err!=nil{
		panic(err)
	}
}
